package org.ibase4j.system.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.ibase4j.system.add.TreeUtil;
import org.ibase4j.system.model.SysMenu;
import org.ibase4j.system.service.SysMenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import top.ibase4j.core.base.BaseController;
import top.ibase4j.core.exception.BusinessException;

import java.util.List;
import java.util.Map;

/**
 * 菜单管理
 *
 * @author ShenHuaJie
 * @version 2016年5月20日 下午3:14:54
 */
@RestController
@Api(value = "菜单管理", description = "菜单管理")
@RequestMapping(value = "menu")
public class SysMenuController extends BaseController<SysMenu, SysMenuService> {

    @ApiOperation(value = "查询菜单")
    @RequestMapping(value = "/read/tree")
    @RequiresPermissions("sys.base.menu.read")
    public Object getTree(ModelMap modelMap, @RequestBody Map<String, Object> param) {
        List<SysMenu> menus = service.queryAllMenus(param);
        List<SysMenu> sysMenuTree = TreeUtil.bulidTree(menus);
        return setSuccessModelMap(modelMap, sysMenuTree);
    }

    @ApiOperation(value = "查询菜单")
    @RequestMapping(value = "/read/tree/withbtns")
    @RequiresPermissions("sys.base.menu.read")
    public Object getTreeWithBtns(ModelMap modelMap, @RequestBody Map<String, Object> param) {
//        List<?> menus = service.queryTreeList(param);
        List<SysMenu> menus = service.queryAllMenus(param);
        List<SysMenu> sysMenuWhtiHandleList = service.queryAllSysMenuWithHandleList(menus);
        List<SysMenu> sysMenuTree = TreeUtil.bulidTree(sysMenuWhtiHandleList);
        return setSuccessModelMap(modelMap, sysMenuTree);
    }

    @Override
    @RequestMapping(value = "/save")
    @ApiOperation(value = "修改菜单")
    @RequiresPermissions("sys.base.menu.update")
    public Object update(ModelMap modelMap, @RequestBody SysMenu param) {
        if (param.getId() != null) {
            SysMenu result = service.queryById(param.getId());
//            if ("1".equals(result.getMenuType())) {
//                throw new BusinessException("不允许修改系统内置菜单");
//            }
        }
        if (param.getIsShow() == null) {
            param.setIsShow("0");
        }
        return super.update(modelMap, param);
    }

    @Override
    @RequestMapping(value = "/delete")
    @ApiOperation(value = "删除菜单")
    @RequiresPermissions("sys.base.menu.delete")
    public Object delete(ModelMap modelMap, @RequestBody SysMenu param) {
        if (param.getId() != null) {
            SysMenu result = service.queryById(param.getId());
            if ("1".equals(result.getMenuType())) {
                throw new BusinessException("不允许删除系统内置菜单");
            }
            SysMenu childParam = new SysMenu();
            childParam.setParentId(param.getId());
            int children =  service.count(childParam);
            if(children > 0) throw new BusinessException("改菜单下还有子菜单,请先删除子菜单!");
        }
        return super.delete(modelMap, param);
    }

    @ApiOperation(value = "获取所有权限")
    @RequiresPermissions("sys.base.menu.read")
    @RequestMapping(value = "/read/permission")
    public Object getPermissions(ModelMap modelMap) {
        List<?> list = service.getPermissions();
        return setSuccessModelMap(modelMap, list);
    }
}
